<?php
// No direct access
defined ('_JEXEC') or die();

jimport ('joomla.application.component.modellist');
jimport('joomla.html.pagination');

class ShipnhanhModelPrepaids extends JModelList
{
	private $columns = array('prepaidid', 'amount', 'date', 'comment');
	
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array('id', 'email', 'amount', 'bank', 'createdDate');
		}
		parent::__construct($config);
	}

	function getListQuery()
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('*');
		$query->from('#__sn_prepaid');

		// Filter search
		$search = $this->getState('filter.search');
		if (!empty($search))
		{
			$search = $db->quote('%'. $db->escape($search, true) . '%');
			$query->where('email LIKE ' . $search);
		}

		$query->order($this->getState('list.ordering', 'id') .
				' ' . $this->getState('list.direction', 'ASC'));

		return $query;
	}
	
	function getList()
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$db->setQuery($this->getListQuery());
		
		$result = $db->loadObjectList();
		
		return $result;
	}
	
	public function isEmailExisted($email)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->select('id');
		$query->from('#__sn_prepaid');
		$search  = $db->quote('%' . $db->escape($email, true) . '%');
		$query->where('email LIKE ' . $search);
		
		$db->setQuery($query);
		$id = $db->loadResult();
		
		return ($id > 0);
	}
	
	public function getPrepaidValue($email)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->select('id, email, amount');
		$query->from('#__sn_prepaid');
		$search  = $db->quote('%' . $db->escape($email, true) . '%');
		$query->where('email LIKE ' . $search);
		
		$db->setQuery($query);
		
		return $db->loadObject();
	}
	
	public function updatePrepaid($id = -1, $amount)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		if ($amount < 0)
			$amount = 0;
		
		$query->update('#__sn_prepaid');
		$query->set('amount=' . $amount);
		$query->where('id=' . $id);
		
		$db->setQuery($query);
		$db->query();
	}
	
	public function insertHistory($prepaidId, $amount, $comment)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$values = array($prepaidId, $amount, 'now()', $db->quote($comment));
		
		$query->insert('#__sn_prepaid_history');
		$query->columns($this->columns);
		$query->values(implode(',', $values));
		$db->setQuery($query);
		$db->query();
	}
	
	public function getHistory($prepaidId)
	{
		$db = JFactory::getDBO();
		$app = JFactory::getApplication();
		
		$stored = $app->getUserState($this->option . '.stored', 0, 'int');
		$lim = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'int');
		
		if ($stored != $lim)
		{
			$start = 0;
			$app->setUserState($this->option . '.stored', $lim);
		}
		else
			$start = JRequest::getVar('limitstart', 0, '', 'int');
		
		
		$db->setQuery('SELECT SQL_CALC_FOUND_ROWS * FROM #__sn_prepaid_history WHERE prepaidid=' . $prepaidId . ' ORDER BY date DESC ' . ' LIMIT ' . $start . ', ' . $lim);
		$list = $db->loadObjectList();
		
		$db->setQuery('SELECT FOUND_ROWS()');
		$pageNav = new JPagination($db->loadResult(), $start, $lim);
		$app->setUserState($this->option . '.pageNav', $pageNav);
		
		return $list;
	}

	protected function populateState($ordering = null, $direction = null)
	{
		// Load the filter state
		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);

		parent::populateState($ordering, $direction);
	}
}